package application;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.geometry.Rectangle2D;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import util.LoadInfoImpl;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

public class Main extends Application {

	private double xOffset = 0;
	private double yOffset = 0;
	
	private Map<String,Long> old;

	@Override
	public void start(Stage primaryStage) {
		try {
			Text text = new Text("");
			text.setFont(new Font(12));
			text.setFill(Color.BLACK);
			
			Text text1 = new Text("");
			text1.setFont(new Font(12));
			text1.setFill(Color.BLACK);
			
			VBox root = new VBox();
			root.getChildren().add(text);
			root.getChildren().add(text1);
			final int width = 150;
			final int height = 25;
			final Scene scene = new Scene(root, width, height);
			scene.setFill(null);
			primaryStage.initStyle(StageStyle.TRANSPARENT);// 隐藏默认标题栏
			scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
			primaryStage.setScene(scene);
			Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
			primaryStage.setX(primaryScreenBounds.getWidth() - 180);
			primaryStage.setY(primaryScreenBounds.getHeight() - 180);
			primaryStage.show();
			root.setOnMousePressed((event) -> {
				event.consume();
				xOffset = event.getSceneX();
				yOffset = event.getSceneY();
			});
			root.setOnMouseDragged((event) -> {
				event.consume();
				primaryStage.setX(event.getScreenX() - xOffset);
				// 根据自己的需求，做不同的判断
				if (event.getScreenY() - yOffset < 0) {
					primaryStage.setY(0);
				} else {
					primaryStage.setY(event.getScreenY() - yOffset);
				}
			});
			Task<?> t = new Task<Object>() {
				@Override
				protected Object call() throws Exception {
					try {
						LoadInfoImpl li = new LoadInfoImpl();
						while (true) {
							String value;
							if(old==null) {
								old = li.getNetworkInfoMap();
								value = "↓0.00B/s▋ ↑0.00B/s";
								text.setText(value);
								System.out.println(value);
							}else {
								Map<String,Long> curMap = li.getNetworkInfoMap();
								String ru = "B/s";
								String tu = "B/s";
								
								double rx = 1.00d*(curMap.get("rx").longValue())-((old.get("rx").longValue()));
								double tx = 1.00d*(curMap.get("tx").longValue())-((old.get("tx").longValue()));
								if(rx>1024) {
									rx = rx/1024;
									ru = "K/s";
								}
								if(rx>1024) {
									rx = rx/1024;
									ru = "M/s";
								}
								if(rx>1024) {
									rx = rx/1024;
									ru = "G/s";
								}
								
								
								if(rx>1024) {
									tx = tx/1024;
									tu = "K/s";
								}
								if(tx>1024) {
									tx = tx/1024;
									tu = "M/s";
								}
								if(tx>1024) {
									tx = tx/1024;
									tu = "G/s";
								}
								
								BigDecimal rbg = new BigDecimal(rx);
								rx = rbg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
								
								BigDecimal tbg = new BigDecimal(tx);
								tx = tbg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
								text.setText("↓" + rx + ru);
								text1.setText("↑"+ tx + tu);
								old = curMap;
							}
							Thread.sleep(1000);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					return "";
				}
			};
			new Thread(t).start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		launch(args);
	}
}
